﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Xml;
using System.Diagnostics;

namespace JetBlack.SimpleTopicBus.Distributor.Config
{
    /// <summary>
    /// A class which captures the configuration settings for the project.
    /// </summary>
    public class Handler : IConfigurationSectionHandler
    {
        private string defaultName = null;
        private Dictionary<string, Adapter> adapters = null;

        /// <summary>
        /// The default configuration name.
        /// </summary>
        public string DefaultName
        {
            get { return defaultName; }
            set { defaultName = value; }
        }

        /// <summary>
        /// A property which returns a named configuration.
        /// </summary>
        /// <param name="name">The configuration name.</param>
        /// <returns>A project configuration.</returns>
        public Adapter this[string name]
        {
            get
            {
                if (adapters.ContainsKey(name))
                    return adapters[name];
                else
                    return null;
            }
        }

        /// <summary>
        /// A property which returns the default configuration.
        /// </summary>
        public Adapter DefaultConfig
        {
            get { return this[defaultName]; }
        }

        /// <inheritdoc/>
        public object Create(object parent, object configContext, XmlNode section)
        {
            if (section == null)
                return null;

#if DEBUG
            string defaultName = "debug";
#else
            string defaultName = "release";
#endif
            if (section.Attributes[defaultName] == null)
            {
                Trace.TraceError("Unable to find configuration default \"{0}\".", defaultName);
                return null;
            }

            this.defaultName = section.Attributes[defaultName].Value;
            if (string.IsNullOrEmpty(this.defaultName))
            {
                Trace.TraceError("Unable to determine default configuration for tag \"{0}\"", defaultName);
                return null;
            }

            this.adapters = CreateAdapters(section.SelectNodes("add"));
            if (this.adapters == null || this.adapters.Count == 0)
            {
                Trace.TraceError("Unable to read configuration for tag \"add\"");
                return null;
            }

            return this;
        }

        private Dictionary<string, Adapter> CreateAdapters(XmlNodeList xmlNodeList)
        {
            Dictionary<string, Adapter> adapters = new Dictionary<string, Adapter>();

            foreach (XmlElement xmlElement in xmlNodeList)
            {
                Adapter adapter = CreateAdapter(xmlElement);
                if (adapter == null)
                {
                    Trace.TraceError("Unable to read configuration for adapters");
                    return null;
                }

                adapters.Add(adapter.Name, adapter);
            }

            return adapters;
        }

        private Adapter CreateAdapter(XmlElement xmlElement)
        {
            string name = xmlElement.GetAttribute("name");
            if (string.IsNullOrEmpty(name))
            {
                Trace.TraceError("Unable to read adapter configuration for tag \"name\"");
                return null;
            }

            int port = -1;
            if (!Int32.TryParse(xmlElement.GetAttribute("port"), out port))
            {
                Trace.TraceError("Unable to read or understand adapter configuration for tag \"port\"");
                return null;
            }

            return new Adapter(name, port);
        }
    }
}
